package com.example.administrator.fastdream.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.ChooseMoneyBean;
import com.example.administrator.fastdream.date.bean.SplashBean;
import com.example.administrator.fastdream.date.event.EventBean;
import com.example.administrator.fastdream.date.presenter.SplashPresenter;
import com.example.administrator.fastdream.date.utils.CacheUtils;
import com.example.administrator.fastdream.view.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChooseMoneyActivity extends BaseActivity implements SplashPresenter.DataCallBack {

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_next)
    TextView tv_next;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<ChooseMoneyBean> list;
    private MoneyAdapter moneyAdapter;
    private SplashPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_money);
        ButterKnife.bind(this);


        presenter = new SplashPresenter(this, this);
        presenter.data("2");
    }

    private boolean isChooseDream() {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isChecked()) {
                CacheUtils.put("dream_money",list.get(i).getContent());
                return true;
            }
        }
        return false;
    }

    @Override
    public void date(SplashBean loginBean) {
        ivBack.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setNestedScrollingEnabled(false);
        list = new ArrayList<>();
        list.add(new ChooseMoneyBean(R.drawable.yiwan, "1万元", loginBean.getData().getNumber_list().get(0).getNumber()));
        list.add(new ChooseMoneyBean(R.drawable.wuwan, "5万元", loginBean.getData().getNumber_list().get(1).getNumber()));
        list.add(new ChooseMoneyBean(R.drawable.shiwuwan, "15万元", loginBean.getData().getNumber_list().get(2).getNumber()));
        list.add(new ChooseMoneyBean(R.drawable.ershiwan, "20万元", loginBean.getData().getNumber_list().get(3).getNumber()));
        moneyAdapter = new MoneyAdapter(R.layout.item_choose_dream, list);
        recyclerView.setAdapter(moneyAdapter);
        moneyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(position).setChecked(!list.get(position).isChecked());
                    } else {
                        list.get(i).setChecked(false);//注意这里是i
                    }
                }
                moneyAdapter.notifyDataSetChanged();
                if (!isChooseDream()) {
                    tv_next.setBackgroundResource(R.drawable.shape_button_unchecked);
                } else {
                    tv_next.setBackgroundResource(R.drawable.shape_button_checked);
                }
            }
        });
    }

    private class MoneyAdapter extends BaseQuickAdapter<ChooseMoneyBean, BaseViewHolder> {

        public MoneyAdapter(int layoutResId, @Nullable List<ChooseMoneyBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, ChooseMoneyBean item) {
            ImageView iv_img = helper.getView(R.id.iv_img);
            iv_img.setImageResource(item.getImage());
            helper.setText(R.id.tv_content, item.getContent());
            ImageView iv_check = helper.getView(R.id.iv_check);
            if (item.isChecked()) {
                iv_check.setImageResource(R.drawable.checked);
            } else {
                iv_check.setImageResource(R.drawable.unchecked);
            }
            helper.addOnClickListener(R.id.iv_check);
            TextView TextStatistics = helper.getView(R.id.Text_Statistics);
            TextStatistics.setText(item.getStatistics() + "人已选");
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_next:
                if (!isChooseDream()) {
                    Toast.makeText(this, "请选择所需金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                EventBean eventBean = new EventBean();
                eventBean.setEventName("finishSplash");
                EventBus.getDefault().postSticky(eventBean);
                startActivity(new Intent(this, MainActivity.class));
                finish();
                break;
        }
    }
}
